package com.hehua.StatePatternExampleTwo;

/**
 * 共享状态模式
 */
public class Switch {
    private static SwitchState currentState, onState, offState; //定义3个静态的状态对象
    private String name;

    public Switch(String name) {
        this.name = name;
        onState = new OnState();
        offState = new OffState();
        currentState = onState;
    }
    public void setState(SwitchState state){
        currentState = state;
    }
    public static SwitchState getState(String type){
        if (type.equalsIgnoreCase("on")){
            return onState;
        }else {
            return offState;
        }
    }
    /**
     * 打开开关
     */
    public void on(){
        System.out.println(name);
        currentState.on(this);
    }
    /**
     * 关闭开关
     */
    public void off(){
        System.out.println(name);
        currentState.off(this);
    }
}
